/*
 *    Copyright © OpenAtom Foundation.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package com.inspur.edp.bef.dtgenerator.api.entityInterface;

import com.inspur.edp.bef.api.be.IBENodeEntity;
import com.inspur.edp.bef.api.be.IBusinessEntity;
import com.inspur.edp.bef.bizentity.GspBizEntityObject;
import com.inspur.edp.bef.dtgenerator.DotNetToJavaStringHelper;
import com.inspur.edp.bef.dtgenerator.api.ApiHelper;
import com.inspur.edp.bef.dtgenerator.api.BefApiGeneratorContext;
import com.inspur.edp.caf.generator.baseInfo.TypeInfo;
import com.inspur.edp.cef.gencommon.MediateType;
import com.inspur.edp.cef.generator.apiGenerator.entity.allInterface.EntityAllInterfaceGenerator;
import com.inspur.edp.cef.generator.overall.Utils;
import com.inspur.edp.das.commonmodel.IGspCommonObject;

public class BeEntityAllInterfaceGenerator extends EntityAllInterfaceGenerator {
  private BefApiGeneratorContext apiGenCtx;
  private GspBizEntityObject beObject;

  public BeEntityAllInterfaceGenerator(
      BefApiGeneratorContext apiGenContext, GspBizEntityObject beObject) {
    super(apiGenContext, beObject);
    this.apiGenCtx = apiGenContext;
    this.beObject = beObject;
    if(beObject.getIsRootNode())
    {
      apiGenContext.setRootEntityAllInterfaceType(new MediateType(apiGenCtx.getNamespace(),getName()));
    }
  }

  @Override
  protected EntityAllInterfaceGenerator createChildEntityGenerator(IGspCommonObject childObject) {
    return new BeEntityAllInterfaceGenerator(apiGenCtx, (GspBizEntityObject) childObject);
  }

  @Override
  protected String getName() {
    return ApiHelper.getBEAllInterfaceClassName(beObject);
  }

  @Override
  protected java.util.ArrayList<com.inspur.edp.caf.generator.baseInfo.TypeInfo>
      addParentInterfaces() {
java.util.ArrayList<com.inspur.edp.caf.generator.baseInfo.TypeInfo> rez =
        super.addParentInterfaces();
    if (rez == null) {
      rez = new java.util.ArrayList<com.inspur.edp.caf.generator.baseInfo.TypeInfo>();
    }
    if (beObject.getIsRef()) // apiGenCtx.getBeGenCtx().BE.HasParentBe()
    {
      // wj-dbe已改
      if (apiGenCtx.getBeGenCtx().getIsParentBEInSameProj()) {
        return null;
      }
      TypeInfo parentClassName = getParentClassName();
      return new java.util.ArrayList<TypeInfo>(
          java.util.Arrays.asList(parentClassName));
    } else {
      if (beObject.getIsRootNode()) {
        rez.add(Utils.createTypeInfo(IBusinessEntity.class));
      } else {
        rez.add(Utils.createTypeInfo(IBENodeEntity.class));
      }
    }
    if (!DotNetToJavaStringHelper.isNullOrEmpty(
            apiGenCtx.getBeGenCtx().getBE().getDependentEntityId())
        && apiGenCtx.getBeGenCtx().getIsParentBEInSameProj()) {
      rez.add(getParentClassName());
    }
    return rez;
  }

  private TypeInfo getParentClassName() {
    MediateType mtype =
        ApiHelper.getBeEntityAllInterfaceClassName(
            apiGenCtx.getBeGenCtx().getParentBE().findObjectByCode(beObject.getCode()));
    TypeInfo result = new TypeInfo(mtype.getClassName());
    result.setTypePackageName(mtype.getNamespace());
    return result;
  }
}
